import { createApp } from 'vue'
import CustomMessage from '@/components/CustomMessage.vue'
import CustomMessageBox from '@/components/CustomMessageBox.vue'
import ElementPlus from 'element-plus'

// 消息提示函数
function showMessage(message, type = 'info', duration = 3000) {
  const messageContainer = document.createElement('div')
  document.body.appendChild(messageContainer)

  const app = createApp(CustomMessage, {
    message,
    type,
    duration,
    onClose: () => {
      app.unmount()
      document.body.removeChild(messageContainer)
    }
  })

  app.use(ElementPlus)
  app.mount(messageContainer)
}

// 对话框函数
function showMessageBox(message, title = '提示', options = {}) {
  return new Promise((resolve, reject) => {
    const messageBoxContainer = document.createElement('div')
    document.body.appendChild(messageBoxContainer)

    const defaultOptions = {
      type: 'info',
      showCancelButton: false,
      showClose: true,
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      closeOnClickModal: true
    }

    const finalOptions = { ...defaultOptions, ...options }

    const app = createApp(CustomMessageBox, {
      message,
      title,
      ...finalOptions,
      onConfirm: () => {
        resolve('confirm')
        cleanup()
      },
      onCancel: () => {
        resolve('cancel')
        cleanup()
      },
      onClose: () => {
        resolve('close')
        cleanup()
      }
    })

    function cleanup() {
      app.unmount()
      document.body.removeChild(messageBoxContainer)
    }

    app.use(ElementPlus)
    app.mount(messageBoxContainer)
  })
}

// 统一的alert函数 - 主要接口
export function alert(message, title = '提示', type = 'info') {
  return showMessageBox(message, title, { type, showCancelButton: false })
}

// 确认对话框
export function confirm(message, title = '确认', type = 'warning') {
  return showMessageBox(message, title, { 
    type, 
    showCancelButton: true,
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
}

// 成功提示
export function success(message, duration = 3000) {
  showMessage(message, 'success', duration)
}

// 错误提示
export function error(message, duration = 4000) {
  showMessage(message, 'error', duration)
}

// 警告提示
export function warning(message, duration = 3500) {
  showMessage(message, 'warning', duration)
}

// 信息提示
export function info(message, duration = 3000) {
  showMessage(message, 'info', duration)
}

// 错误对话框
export function errorAlert(message, title = '错误') {
  return showMessageBox(message, title, { type: 'error', showCancelButton: false })
}

// 警告对话框
export function warningAlert(message, title = '警告') {
  return showMessageBox(message, title, { type: 'warning', showCancelButton: false })
}

// 成功对话框
export function successAlert(message, title = '成功') {
  return showMessageBox(message, title, { type: 'success', showCancelButton: false })
}

// 信息对话框
export function infoAlert(message, title = '提示') {
  return showMessageBox(message, title, { type: 'info', showCancelButton: false })
}

// 默认导出alert函数，方便直接使用
export default alert